//
//  MoreCommentViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2018/1/19.
//  Copyright © 2018年 Lwx. All rights reserved.
//

#import "TravelNoteMoreCommentViewController.h"
#import "MoreCommentTableViewCell.h"
#import "VisitorCommentCellModel.h"
@interface TravelNoteMoreCommentViewController ()<UITableViewDelegate, UITableViewDataSource, MoreCommentCellDelegate>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)VisitorCommentCellModel * model;
@property (nonatomic, strong)VisitorCommentCellModelData * modelData;
@property (nonatomic, assign)int currentPage;
@property (nonatomic, strong)NSMutableArray * dataArra;
@end

@implementation TravelNoteMoreCommentViewController(Networking)
- (void)getCommentListData{
    NSDictionary * dic = @{
                           @"did":@(self.did),
                           @"page":@(self.currentPage),
                           @"row":@(6),
                           @"type":@(1)
                           };
    WS(ws)
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/diary/diaryComment") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (error) {
            [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000){
                if (ws.currentPage == 1) {
                    [self.dataArra removeAllObjects];
                }
                ws.model = [VisitorCommentCellModel yy_modelWithDictionary:obj];
                [self.dataArra addObjectsFromArray:ws.model.data];
                [self.tableView reloadData];
            }else{
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }
        }
    }];
}

@end

@implementation TravelNoteMoreCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"更多评论";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self getCommentListData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = getColor(bgColor);
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.currentPage = 1;
            [self getCommentListData];
        }];
        
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.currentPage ++;
            [self getCommentListData];
        }];
        [self.tableView.mj_header beginRefreshing];
        
    }return _tableView;
}

- (NSMutableArray *)dataArra{
    if (!_dataArra) {
        _dataArra = [NSMutableArray array];
    }return _dataArra;
}

#pragma mark - delegate和dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArra.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"moreComment";
    [self.tableView registerClass:[MoreCommentTableViewCell class] forCellReuseIdentifier:cellID];
    MoreCommentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if (!cell) {
        cell = [[MoreCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.modelData = self.dataArra[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat labelBottomY = [self getLabelHeightWithText:((VisitorCommentCellModelData *)self.dataArra[indexPath.row]).content width:SCREEN_WIDTH - 20 font:14];
    if (((VisitorCommentCellModelData *)self.dataArra[indexPath.row]).pic1 != nil && ((VisitorCommentCellModelData *)self.dataArra[indexPath.row]).pic1.length > 0) {
        return labelBottomY + 180;
    }
    return labelBottomY + 80;
}

//自适应Lable高度
- (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFloat)font
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    return rect.size.height;
}

@end
